const YoloDetector = require('../yolo');
const APP = require('../APP');
class index {
    androidBot = null;
    done = false;
    constructor(client) {
        this.GameClient = client;
        this.androidBot = this.GameClient.androidBot;
    }
    async run() {
        let isRun = await this.androidBot.appIsRunnig('com.tencent.jkchess');
        if (!isRun) {
            console.log('游戏未运行');
            this.GameClient.addTask({ name: 'clearAll' });
            this.GameClient.addTask({ name: 'startGame' });
            return true;
        }
        // 分析页面元素判断所处状态
        let data = await YoloDetector.screen(this.androidBot);
        await this.analysisPageState(data);
        return true;
    }
    async check_view_login(yoloData, ocrData) {
        if (this.done) {
            return true;
        }
        if (APP.findClass(yoloData, 'login_btn_startGame')) {
            this.done = true;
            this.GameClient.addTask({ name: 'login' });
            return true;
        }
        let wx_ios = APP.ocrDataFindTextArray(ocrData, ['微信', 'qq', '金铲铲']);
        if (wx_ios) {
            this.done = true;
            this.GameClient.addTask({ name: 'login' });
            return true;
        }
        return false;
    }
    /**
     * 扫码界面 检测
     */
    async check_view_scan(ocrData) {
        if (this.done) {
            return true;
        }
        let 授权登录 = APP.ocrDataFindText(ocrData, '授权登录');
        if (授权登录) {
            this.done = true;
            this.GameClient.addTask({ name: 'login/login_san' });
            return true;
        }
        return false;
    }
    async analysisPageState(data) {
        let words = await APP.ocrRegion(this.androidBot);
        // 处理断言页面

        // 登录界面
        await this.check_view_login(data, words);
        // 扫码界面
        await this.check_view_scan(words);

        if (this.done) {
            return true;
        }
        await APP.sleep(1000);
        this.GameClient.addTask({ name: 'analysisPageState' })
        console.log('ocr识别', words);
        debugger;
        return;
        // 分析页面元素判断所处状态
        let havePop = APP.findClass(data, 'pop');
        if (havePop) {
            await this.analysisPop(data);
            return;
        }
        let isLogin = APP.findClass(data, 'page_login');

    }
    async analysisPop(data) {
        // 分析弹窗
        let words = await this.androidBot.getWords();
        console.log('ocr识别', words);
        debugger;
        let pop = APP.findClass(data, 'pop');
        let pop_close = APP.findClass(data, 'pop_close');
        if (pop_close) {
            APP.clickRegion(this.androidBot, data[pop_close - 1].region);

        }
    }
}

module.exports = index;